﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabDayAtRaces
{
    public partial class Form1 : Form
    {
        Greyhound[] GreyhoundArray = new Greyhound[4];
        Guy[] GuyArray = new Guy[3];
        Random MyRandomizer = new Random();
        

        public Form1()
        {
            
            InitializeComponent();

            //initialize the greyhounds
            GreyhoundArray[0] = new Greyhound()
            {
                MyPictureBox = pictureBoxDog1,
                StartingPosition = pictureBoxDog1.Left,
                RacetrackLength = racetrack.Width - pictureBoxDog1.Width,
                Randomizer = MyRandomizer
            };
            GreyhoundArray[1] = new Greyhound()
            {
                MyPictureBox = pictureBoxDog2,
                StartingPosition = pictureBoxDog2.Left,
                RacetrackLength = racetrack.Width - pictureBoxDog2.Width,
                Randomizer = MyRandomizer
            };
            GreyhoundArray[2] = new Greyhound()
            {
                MyPictureBox = pictureBoxDog3,
                StartingPosition = pictureBoxDog3.Left,
                RacetrackLength = racetrack.Width - pictureBoxDog3.Width,
                Randomizer = MyRandomizer
            };
            GreyhoundArray[3] = new Greyhound()
            {
                MyPictureBox = pictureBoxDog4,
                StartingPosition = pictureBoxDog4.Left,
                RacetrackLength = racetrack.Width - pictureBoxDog4.Width,
                Randomizer = MyRandomizer
            };
            //initialize the guys
            GuyArray[0] = new Guy()
            {
                Name = "Joe",
                Cash = 50,
                MyBet = new Bet(),
                MyRadioButton = joeRadioButton,
                MyLabel = joeBetLabel
            };
            GuyArray[1] = new Guy()
            {
                Name = "Bob",
                Cash = 75,
                MyBet = new Bet(),
                MyRadioButton = bobRadioButton,
                MyLabel = bobBetLabel
            };
            GuyArray[2] = new Guy()
            {
                Name = "Al",
                Cash = 45,
                MyBet = new Bet(),
                MyRadioButton = alRadioButton,
                MyLabel = alBetLabel
            };

            //Set initial label text
            minimumBetLabel.Text = "Minimum Bet: " + Bet.minimumBet;
            for (int i = 0; i < GuyArray.Length; i++)
            {
                GuyArray[i].ClearBet();
                GuyArray[i].UpdateLabels();
            }

        }

        private void raceButton_Click(object sender, EventArgs e)
        {
            
            timer1.Start();
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Racing section
            
            //disable any more betting or starting the race again while the race is happening
            bettingParlor.Enabled = false;

            for (int i = 0; i < GreyhoundArray.Length; i++)//Start race
            {    
                

                if (GreyhoundArray[i].Run())//is the race over?
                {
                    timer1.Stop();//end race

                    MessageBox.Show("Dog " + (i + 1) + " wins!");//this dog wins message
                    //Calculation for the winners
                    for (int ii = 0; ii < GuyArray.Length; ii++)
                    {
                        GuyArray[ii].collect(i);
                    }

                    
                    // Enable the betting Parlor so more bets can be placed and another race started.
                    bettingParlor.Enabled = true;
                    // Reset starting line
                    for (int ii = 0; ii < GreyhoundArray.Length; ii++)
                    {
                        GreyhoundArray[ii].TakeStartingPosition();
                    }

                    i = GreyhoundArray.Length; // end the loop (timer may have stopped at 0 which means after the reset 1,2,3 will still move
                }
            }
        }

        private void betsButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GuyArray.Length; i++)
            {
                if (GuyArray[i].MyRadioButton.Checked)
                {
                    if(GuyArray[i].PlaceBet((int)betAmountNumericUpDown.Value, (int)dogNumberNumericUpDown.Value))
                    {
                        GuyArray[i].UpdateLabels();
                    }
                }
                
            }
        }

        private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            nameLabel.Text = GuyArray[0].Name;
        }

        private void bobRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            nameLabel.Text = GuyArray[1].Name;
        }

        private void alRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            nameLabel.Text = GuyArray[2].Name;
        }
    }
}
